package com.duoduo.selenium.demo;

import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import com.duoduo.selenium.util.SeleniumUtil;

public class WindowsTest {

	private static WebDriver driver = null;

	public static void main2(String[] args) {
		SeleniumUtil.setProperties();

		driver = new FirefoxDriver();
		driver.get("http://www.baidu.com");
		driver.manage().window().maximize();

		String searchHandle = driver.getWindowHandle();
		System.out.println("baidu search handle : " + searchHandle);

		// 获取百度新闻的连接，然后利用js打开一个新的窗口
		SeleniumUtil.waitme(3000);
		String href = driver.findElement(By.cssSelector("[name=tj_trnews]")).getAttribute("href");
		System.out.println("the link of news is : " + href);
		jsOpen(href);

		SeleniumUtil.waitme(10000);
		jsOpen("http://www.iteye.com");

		Set<String> handles = driver.getWindowHandles();

		// 进入百度新闻窗口,并获取title验证
		SeleniumUtil.waitme(10000);
		for (String handle : handles) {
			if (!handle.equals(searchHandle)) {
				driver.switchTo().window(handle);
				System.out.println("current url>>" + driver.getCurrentUrl());
				if (driver.getTitle().contains("百度新闻")) {
					System.out.println("switch to news page successfully");
				}
				break;
			}
		}

		// 返回百度首页
		SeleniumUtil.waitme(3000);
		driver.switchTo().window(searchHandle);
		System.out.println("current url>>" + driver.getCurrentUrl());
		if (driver.getTitle().contains("百度一下")) {
			System.out.println("switch to search page successfully");
			driver.findElement(By.cssSelector("#kw")).sendKeys("switch successfully");
		}

		// SeleniumUtil.waitme(3000);
		// driver.close();
	}

	private static void jsOpen(String url) {
		JavascriptExecutor executor = (JavascriptExecutor) driver;
		if (StringUtils.isNotBlank(url)) {
			executor.executeScript("window.open('" + url + "')");
		} else {
			executor.executeScript("window.open()");
		}
	}

	public static void main(String[] args) {

		SeleniumUtil.setProperties();

		driver = new FirefoxDriver();
		driver.get("https://www.baidu.com/");
		String seach_hander = driver.getWindowHandle();
		System.out.println(seach_hander);

		driver.manage().timeouts().setScriptTimeout(100, TimeUnit.SECONDS);
		// driver.findElement(By.linkText("登录")).click();
		driver.findElement(By.xpath("//div[@id='u1']/a[@name='tj_login']")).click();
		SeleniumUtil.waitme(10000);

		// driver.findElement(By.linkText("立即注册")).click();
		JavascriptExecutor executor = (JavascriptExecutor) driver;
		executor.executeScript("window.open($('.pass-reglink').attr('href'))");
		SeleniumUtil.waitme(5000);

		Set<String> handles = driver.getWindowHandles();
		for (String handle : handles) {
			if (!handle.equals(seach_hander)) {
				driver.switchTo().window(handle);
				System.out.println("current url>>" + driver.getCurrentUrl());
				break;
			}
		}
		SeleniumUtil.waitme(5000);

		// driver.findElement(By.xpath("//input[@name='userName']")).sendKeys("ruibo.zhong");
		// driver.findElement(By.xpath("//input[@name='phone']")).sendKeys("15202162713");
		// driver.findElement(By.xpath("//input[@name='verifyCode']")).sendKeys("888888");
		// driver.findElement(By.xpath("//input[@name='password']")).sendKeys("888888");
	}

}
